<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Articles extends Controller_Common {
	public $template='layout/index';

	public function action_index() {
		$per_page = $this->_config->get('items_per_page');
		$posts = Model::factory('Articles')->getSome($this->getType($this->request->param('type')), $this->getOffset($per_page));

		if (!empty($posts)) {
			$this->buildCarcas();
			$this->template->title = $this->sitetext[$this->request->param('type')]['_value'].' - '.$this->sitetext['comapny_name']['_value'];

			$this->template->content = View::factory('pages/articles', array(
				'left_menu' => Model::factory('Menu')->get_by_location(1),
				'posts' => $posts,
				'right_banners' => Model::factory('Banners')->get_right(),
				'bot_banner' => Model::factory('Banners')->get_bot(),
				'partners' => Model::factory('Partners')->get_all(),
				'items_count' => count($posts),
	            'pagination' => Pagination::factory(array(
	            	'view' => 'pagination/basic_ru',
	                'total_items' => Model::factory('articles')->countAll($this->getType($this->request->param('type'))),
	                'items_per_page' => $this->_config->get('items_per_page'),
	            )),
				'breadcrumbs' => View::factory('pieces/breadcrumbs', array(
					'breadcrumbs' => array(
						array('active' => 0, 'title' => 'Главная', 'link' => '/'),
						array('active' => 1, 'title' => 'Посты', 'link' => '/posts')
					)
				)),
			));
		} else {
			throw new HTTP_Exception_404(':page does not exist', array(':page' => 'Gaia'));
		}
	}

	public function action_one() {
		$article = Model::factory('Articles')->getOne($this->request->param('id'));

		if (!empty($article)) {
			$this->buildCarcas();
			$this->template->title = $article->title.' - '.$this->sitetext[$this->request->param('type')]['_value'].' - '.$this->sitetext['comapny_name']['_value'];
			$this->template->description = $article->meta_description;
			$this->template->content = View::factory('pages/article', array(
				'article' => $article,
				'left_menu' => Model::factory('Menu')->get_by_location(1),
				'right_banners' => Model::factory('Banners')->get_right(),
				'bot_banner' => Model::factory('Banners')->get_bot(),
				'partners' => Model::factory('Partners')->get_all(),
				'breadcrumbs' => View::factory('pieces/breadcrumbs', array(
					'breadcrumbs' => array(
						array('active' => 0, 'title' => 'Главная', 'link' => '/'),
						array('active' => 0, 'title' => 'Посты', 'link' => '/posts'),
						array('active' => 1, 'title' => $article->title, 'link' => '/posts/'.$article->id),
					)
				)),
			));
		} else {
			throw new HTTP_Exception_404(':page does not exist', array(':page' => 'Gaia'));
		}
	}

}